package com.wj.book.mapper;

import com.wj.book.pojo.Book;
import org.apache.ibatis.jdbc.SQL;

public class SqlContext {
    //构造动态查询图书的SQL语句
    public String searchBookSql(Book book){
        return new SQL(){
            { SELECT("*");
                FROM("book");
                if(book.getName()!=null&&book.getName()!=""){
                    WHERE(" name like '%' #{name} '%'");
                }
                if(book.getCategory()!=null&&book.getCategory()!=""){
                    WHERE(" category=#{category}");
                }
                if(book.getAuthor()!=null&&book.getAuthor()!=""){
                    WHERE(" author=#{author}");
                }
            }
        }.toString();
    }
}
